var totalPage;
var type = 0;
var c = require('checkAuthority');
var s = require('selectTransformat');
var m = require('model');
/*准确查询*/
function pageChange(page) {
	$.ajax({
		type: "post",
		url: "./order/searchorder.bto",
		async: false,
		data: {
			page: page
		},
		success: function(data) {
			s.transformatStatus();
			var authorityCode = data.code;
			function template() {
				var myTemplate = __inline('declareManage.handlebars');
				$('#table').html(myTemplate(data.other.tbo.value.value));
				totalPage = data.other.tbo.value.totalData;
				btnEdit();
			}
			c.authority(authorityCode, template);
		}
	});
}

/*删除数据*/
function delDeclare(delId) {
	$('.btnSure').on('click', function() {
		$.ajax({
			type: 'post',
			url: './order/removeorder.bto?orderid=' + delId,
			success: function(data) {
				window.location.href = "declareManage.html"
			}
		});
	});
}

/*模糊查询事件*/
$('#keyword').keyup(function() {
	var type1 = $('#type').val();
	var keyword = $(this).val();
	keywordPageChange(type1, keyword);
	if (totalPage != 0) {
		$.jqPaginator('#pagination1', {
			totalCounts: totalPage,
			pageSize: 10,
			onPageChange: function(num, type) {
				keywordPageChange(type1, keyword);
			}
		});
	}

});

/*模糊查询*/
function keywordPageChange(type, keyword) {
	$.ajax({
		type: "post",
		url: "./order/keywordsearch.bto",
		async: false,
		data: {
			page: 1,
			keyword: keyword,
			type: type
		},
		success: function(data) {
			s.transformatStatus();
			var myTemplate = __inline('declareManage.handlebars');
			$('#table').html(myTemplate(data.other.tbo.value));
			totalPage = data.other.tbo.totalData;
			btnEdit();
		}
	});
}

/*列表按钮遍历处理*/
function btnEdit() {
	$('tbody td:last-of-type').each(function() {
		var editDeclareBtn = $(this).children('.editDeclare');
		var delDeclareBtn = $(this).children('.delDeclare');
		editDeclareBtn.on('click', function() {
			var editId = $(this).attr('data-id');
			var customsDeclaration = $(this).attr('data-id2');
			window.location.href = "editDeclare.html?id=" + editId + "&customsDeclaration=" + customsDeclaration;
		});
		delDeclareBtn.on('click', function() {
			var delId = $(this).attr("data-id"); //获取单列的
			m.model('删除报关', '#delDeclareModal');
			delDeclare(delId);
		});
	});
}

/*分页插件*/
pageChange(1);
$.jqPaginator('#pagination1', {
	totalCounts: totalPage,
	pageSize: 10,
	onPageChange: function(num, type) {
		pageChange(num);
	}
});